package com.colorlight.cloud.complete.entity.instructions;

import com.colorlight.cloud.common.exception.ParameterException;
import com.colorlight.cloud.common.model.ExceptionEnum;
import com.colorlight.cloud.complete.api.CompleteEntityScheduleActions;
import com.colorlight.iot.proto.complete.instruction.CompleteInstruction;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;

/**
 * 删除屏体
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Slf4j
public class DeleteScreen implements CompleteEntityScheduleActions {

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Builder
    public static class InputMsg implements Serializable {
        /**
         * 屏体uuid
         */
        private List<String> uuid;
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Builder
    public static class OutputMsg implements Serializable {
        /**
         * 返回删除的屏体uuid
         */
        private List<String> uuid;

        /**
         * 是否删除成功
         */
        private boolean result;
    }

    private InputMsg inputMsg;
    private OutputMsg outputMsg;

    @Override
    public ByteString objectConvertBytes() {
        final CompleteInstruction.DeleteScreen.InputMsg.Builder inputMsgBuilder =
                CompleteInstruction.DeleteScreen.InputMsg.newBuilder();
        DeleteScreen.InputMsg thisInput = this.getInputMsg();
        if(Objects.nonNull(thisInput) && Objects.nonNull(thisInput.getUuid())){
            inputMsgBuilder.addAllUuid(thisInput.getUuid());
        }
        final CompleteInstruction.DeleteScreen.Builder builder =
                CompleteInstruction.DeleteScreen.newBuilder();
        builder.setInput(inputMsgBuilder);
        return builder.build().toByteString();
    }

    @Override
    public DeleteScreen byteConvertObject(ByteString bytes) {
        CompleteInstruction.DeleteScreen deleteScreen = null;
        try {
            deleteScreen = CompleteInstruction.DeleteScreen.parseFrom(bytes);
        } catch (InvalidProtocolBufferException e) {
            log.error("", e);
        }
        if (Objects.isNull(deleteScreen)) {
            throw new ParameterException(ExceptionEnum.MSG_PARAMETER_EXCEPTION);
        }
        DeleteScreen.OutputMsg outputMsg = new DeleteScreen.OutputMsg();
        outputMsg.setUuid(deleteScreen.getOutput().getUuidList());
        outputMsg.setResult(deleteScreen.getOutput().getResult());
        this.setOutputMsg(outputMsg);
        return this;
    }
}

